package com.cskaoyan.service;

import com.cskaoyan.bean.AdminExample;
import com.cskaoyan.bean.ListData;
import com.cskaoyan.bean.Role;
import com.cskaoyan.bean.RoleExample;
import com.cskaoyan.bean.bo.AdminRoleVO;
import com.cskaoyan.bean.bo.PermissionBO;
import com.cskaoyan.bean.vo.PermissionFirstChildVO;
import com.cskaoyan.bean.vo.PermissionFourthChildVO;
import com.cskaoyan.bean.vo.PermissionSecondChildVO;
import com.cskaoyan.bean.vo.PermissionThirdChildVO;
import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.mapper.RoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author heyyon
 */
@Service
public class AdminRoleServiceImpl implements AdminRoleService{
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    AdminMapper adminMapper;
    @Override
    public List<AdminRoleVO> options() {
        List<AdminRoleVO> adminRoleList=roleMapper.selectRole();
        return adminRoleList;
    }

    @Override
    public ListData<Role> list(Integer page, Integer limit,String name, String sort, String order) {
        PageHelper.startPage(page, limit);
        RoleExample roleExample = new RoleExample();
        roleExample.setOrderByClause(sort+" "+order);
        RoleExample.Criteria criteria=roleExample.createCriteria();
        if (name != null) {
            criteria.andNameLike("%"+name+"%");
        }
        List<Role> roleList = roleMapper.selectByExample(roleExample);
        PageInfo<Role> rolePageInfo = new PageInfo<>(roleList);
        long total=rolePageInfo.getTotal();
        ListData<Role> roleListData=new ListData<>();
        roleListData.setItems(roleList);
        roleListData.setTotal((int) total);
        return roleListData;
    }

    @Override
    public Role create(Role role) {
        roleMapper.insertSelective(role);
        return role;
    }

    @Override
    public int update(Role role) {
        int row = roleMapper.updateByLatestRole(role);
        return row;
    }
    //删除角色时，先判断该角色下是否有管理员，再进行删除
    @Override
    public int delete(Role role) {
        int roleId=role.getId();
        String roleIdStr = String.valueOf(roleId);
        List<String> roleIdList=adminMapper.selectRoleIds();
        for (String s : roleIdList) {
            if (s.startsWith("["+roleIdStr + ",") || s.endsWith("," + roleIdStr+"]") || s.contains("," + roleIdStr + ",")) {
                return -1;
            }
        }
        int row = roleMapper.deleteByPrimaryKey(roleId);
        return row;
    }

    /**
     * @param roleId 
     * @return PermissionFirstChildVO
     * 没有用上课讲的那种比较牛的查询，这里用的是分次查询，分了好多次,笨方法
     */
    @Override
    public PermissionFirstChildVO permissions(Integer roleId) {
        PermissionFirstChildVO permissionFirstChildVO = new PermissionFirstChildVO();
        AdminExample adminExample=new AdminExample();
        AdminExample.Criteria criteria=adminExample.createCriteria();
        
        //查出权限类目的id
        List<Integer> categoryIds = roleMapper.selectCategoryIds();
        List<PermissionSecondChildVO> secondChildList=new ArrayList<>();
        for (Integer categoryId : categoryIds) {
            //按照权限类目categoryId来遍历查询，并封装其id和label
            PermissionSecondChildVO permissionSecondChildVO=roleMapper.selectSecondChildInfo(categoryId);
            //按照权限类目id查找其子类目的权限的名称
            List<String> categoryThirdChildNames = roleMapper.selectThirdChildNames(categoryId);
            List<PermissionThirdChildVO> thirdChildList=new ArrayList<>();
            //categoryThirdChildName对应表cskaoyanmall_permission_detail中的name
            for (String categoryThirdChildName : categoryThirdChildNames) {
                PermissionThirdChildVO permissionThirdChildVO;
                permissionThirdChildVO=new PermissionThirdChildVO();
                permissionThirdChildVO.setId(categoryThirdChildName);
                permissionThirdChildVO.setLabel(categoryThirdChildName);
                List<PermissionFourthChildVO> fourthChild = roleMapper.selectFourthChildInfo(categoryThirdChildName);
                permissionThirdChildVO.setChildren(fourthChild);
                thirdChildList.add(permissionThirdChildVO);
            }
            permissionSecondChildVO.setChildren(thirdChildList);
            secondChildList.add(permissionSecondChildVO);
        }
        permissionFirstChildVO.setSystemPermissions(secondChildList);
        List<String> assignedPermissions = roleMapper.selectAssignedPermissions(roleId);
        permissionFirstChildVO.setAssignedPermissions(assignedPermissions);
        return permissionFirstChildVO;
    }

    @Override
    public int permissions(PermissionBO permissionBO) {
        List<String> permissions=permissionBO.getPermissions();
        int i = roleMapper.deletePermissionByRoleId(permissionBO.getRoleId());
        int sum=0;
        for (String permission : permissions) {
            roleMapper.insertRoleIdAndPermission(permissionBO.getRoleId(), permission);
            ++sum;
        }
        return sum;
    }
}
